package com.cskaoyan.service;

import com.cskaoyan.bean.baseresultVO.WxResultListVo;
import com.cskaoyan.bean.domain.Goods;
import com.cskaoyan.bean.domain.GoodsExample;
import com.cskaoyan.bean.domain.Topic;
import com.cskaoyan.bean.domain.TopicExample;

import com.cskaoyan.bean.wxTopic.WxDetailVo;
import com.cskaoyan.bean.wxTopic.WxTopicListVo;
import com.cskaoyan.bean.wxTopic.WxTopicVo;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.TopicMapper;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Author lirui
 * @Date 2021/12/2 17:45
 * @describe:
 */
@Service
public class WxTopicServicImpl implements WxTopicService {
    @Autowired
    TopicMapper topicMapper;
    @Autowired
    GoodsMapper goodsMapper;
    


    @Override
    public WxTopicListVo getTopicList(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        TopicExample topicExample = new TopicExample();
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        ArrayList<WxTopicVo> topicVos=new ArrayList<>();
        for (Topic topic:topics){
            WxTopicVo wxTopicVo = new WxTopicVo();
           wxTopicVo.setId(topic.getId());
           wxTopicVo.setTitle(topic.getTitle());
           wxTopicVo.setSubtitle(topic.getSubtitle());
           wxTopicVo.setPrice(topic.getPrice());
           wxTopicVo.setReadCount(topic.getReadCount());
           wxTopicVo.setPicUrl(topic.getPicUrl());
           topicVos.add(wxTopicVo);

        }
        PageInfo info = new PageInfo(topics);
        Integer pages=info.getPages();
        Integer total=(int)info.getTotal();
        WxTopicListVo wxTopicListVo=new WxTopicListVo();
        wxTopicListVo.setList(topicVos);
        wxTopicListVo.setLimit(limit);
        wxTopicListVo.setPage(page);
        wxTopicListVo.setPages(pages);
        wxTopicListVo.setTotal(total);
        return wxTopicListVo;
    }

    @Override
    public WxDetailVo queryTopic(Integer id) {

        ArrayList<Goods> goods2 = new ArrayList<>();

        WxDetailVo wxDetailVo = new WxDetailVo();
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        Integer[] goods=topics.get(0).getGoods();
        if (goods!=null){
            for (int i = 0; i <goods.length ; i++) {

                GoodsExample goodsExample = new GoodsExample();
                GoodsExample.Criteria criteria1 = goodsExample.createCriteria();
                criteria1.andIdEqualTo(goods[i]);
                List<Goods> goods1 = goodsMapper.selectByExample(goodsExample);
                goods2.add(goods1.get(0));

            }

        }

        wxDetailVo.setTopic(topics.get(0));
       wxDetailVo.setGoods(goods2);
        return wxDetailVo;

    }

    @Override
    public WxResultListVo queryRelatedList(Integer id) {
        TopicExample topicExample = new TopicExample();
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        ArrayList<Integer> ids = new ArrayList<>();

        for (Topic relatedTopic:topics){
            Integer id1 = relatedTopic.getId();
            ids.add(id1) ;
        }
        ids.remove(id);
        int size = ids.size();
        Random random = new Random();
        ArrayList<Topic> topicVo = new ArrayList<>();
        while (topicVo.size()<=4){
            int i = random.nextInt(size);
            if (!topicVo.contains(topics.get(1))){
                topicVo.add(topics.get(i));
            }
        }
        PageInfo info = new PageInfo(topicVo);
        Integer total=(int)info.getTotal();
        Integer pages=info.getPages();

        WxResultListVo wxResultListVo=new WxResultListVo(topicVo,4,1,pages,total);

        return wxResultListVo;
    }
}
